package cz.proks.bank.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.proks.bank.dao.OperationLogDao;
import cz.proks.bank.domain.Account;
import cz.proks.bank.domain.OperationLog;

@Repository
@Transactional
@Scope("prototype")
public class OperationLogDaoImpl extends BaseGenericDaoImpl<OperationLog, Long> implements OperationLogDao {
	private final Logger log = Logger.getLogger(getClass());
	
	@PersistenceContext
	private EntityManager entityManager;
	
	public OperationLogDaoImpl() {
		super(OperationLog.class);
	}

	@Override
	@Transactional(readOnly = false)
	public List<OperationLog> listByAccount(Account account) {
		log.debug("listByAccount():: Start with account id '" + account + "'");
		
		Query query = entityManager.createNamedQuery("OperationLog.listByAccount");
		query.setParameter("account", account);
		List<OperationLog> logs = query.getResultList();
		
		log.debug("listByAccount():: Exit with " + (logs == null ? "null" : logs.size()) + " log records");
		return logs;
	}	
}
